using UnityEngine;
using System.Collections;

public class Coin : DrawableGameObject {
	
	public float xVelocity;
	public Texture2D[] coinAnimation;
	public Texture2D[] catchAnimation;
	
	private PlayerMechanic playerMechanic;
	
	private bool catched = false;
	
	public Vector3 shineScale;
	public int shineFPS;
	
	private int effectIndex = 0;
	
	public override void Start()
	{
		base.Start();
		
		this.playerMechanic = GameObject.FindGameObjectWithTag(Tags.PLAYER).GetComponent<PlayerMechanic>();
		
		SetAnimations(coinAnimation, catchAnimation, null, null, null);
	}
	
	void Update () 
	{
		if(this.catched)
		{
			if(!gameAnimation.isPlaying())
			{
				Destroy(this.gameObject);
			}
			
		}else
		{
			RunAnimation("First", "Loop");
		}

		this.gameObject.transform.Translate(new Vector3(this.xVelocity * PlayerMechanic.gameVel, 0, 0) * Time.deltaTime);
		
	}
	
	void OnTriggerEnter(Collider other)
	{
		if(other.tag.Equals(Tags.PLAYER))
		{				
			if(!this.catched){
				
			GameObject.FindGameObjectWithTag(Tags.SCORE_COIN).GetComponent<ScoreManager>().AddScore();
				
			this.gameAnimation.fps = this.shineFPS;
			this.gameObject.transform.localScale = shineScale;
			this.catched = true;
			RunAnimation("Second", "OneShot");
            AudioPlayer.Play(AudioPlayer.COIN);
			}
		}

        if(other.tag.Equals(Tags.LIMIT))
        {
            Destroy(this.gameObject);
        }
	}
}
